﻿using SqlSugar;
using System.Reflection;

namespace XieCan.Core.EntitySync
{
  internal class Program
  {
    static void Main(string[] args)
    {
      var db = new SqlSugarClient(new ConnectionConfig()
      {
        ConnectionString = "server=localhost;Database=ApiTest;Uid=root;Pwd=root;",
        DbType = DbType.MySql,
        IsAutoCloseConnection = true,
        InitKeyType = InitKeyType.Attribute
      });

      #region 对话框
      Console.WriteLine("是否确定同步数据库表结构？(同名数据表将会被备份，生产环境慎用，回车确认)");
      var str = Console.ReadKey();
      if (str.Key == ConsoleKey.Enter)
      {
        Console.WriteLine("同步中，请稍后...");
      }
      else
      {
        Console.WriteLine("\r\n输入错误，已退出...");
        return;
      }
      #endregion

      //同步数据表结构
      db.DbMaintenance.CreateDatabase();

      db.CodeFirst.SetStringDefaultLength(50).BackupTable()
        .InitTables(Assembly.Load("XieCan.Core.Entity").GetTypes().Where(t => t.BaseType?.Name == "BaseEntity").ToArray());
      Console.WriteLine("数据库结构同步完成!");
    }
  }
}
